﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;


namespace MyVocationSpace
{
    public partial class AddMyVocation : System.Web.UI.Page
    {
        //static ArrayList arr = new ArrayList();

        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
            if (Session["UserID"] == null)
            {
                Response.Redirect("Home.aspx");
            }
            if (!IsPostBack)
            {
                int Id = Convert.ToInt32(Session["UserID"]);
                BAL.Industry.PopulateIndustry(ref ddlIndustry);
                BindListVocation();
                PopulateIndustry();
                ListBoxBindDetails();
                HasIndustry();

                //ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "call me", "VocationBtnEnable()", true);
            }
           
        }



        public void HasIndustry()
        {
            int flag = BAL.Users.HasIndustry(Convert.ToInt32(Session["UserID"]));
            if (flag == 1)
            {
                btnAddItem.Visible= true;
            }
            else
            {
                btnAddItem.Visible = false;
            }
        }


        public void PopulateIndustry()
        {
            string[] detail = BAL.Users.GetUserDetails(Convert.ToInt32(Session["UserID"]));

            if (detail[17] != "0")
            {
                ListItem lst = ddlIndustry.Items.FindByValue(detail[17]);
                if (lst.Value != null)
                {
                    ddlIndustry.SelectedValue = lst.Value;
                }
            }
        }
        public void ListBoxBindDetails()
        {
            int Id = Convert.ToInt32(Session["UserID"]);
            DataSet ds = BAL.Vocation.SelectVocationDetails(Id);
            lstVocation2.DataSource = ds;
            lstVocation2.DataTextField = "VocationName";
            lstVocation2.DataValueField = "IDVocation";
            lstVocation2.DataBind();
        }
        public void BindListVocation()
        {
            DataSet ds = BAL.Industry.GetAllVocation();
            lstVocation1.DataSource = ds;
            //System.Globalization.CultureInfo.CurrentUICulture.TextInfo.ToTitleCase(lstVocation1.DataTextField) = "VocationName";
            lstVocation1.DataTextField = "VocationName";
            lstVocation1.DataValueField = "IDVocation";
            lstVocation1.DataBind();
        }
        protected void btnAddItem_Click(object sender, EventArgs e)
        {
            int count = lstVocation2.Items.Count;
            foreach (ListItem item in lstVocation1.Items)
            {
                if (item.Selected)
                {
                    if (!lstVocation2.Items.Contains(item))
                    {
                        if (count < 5)
                        {
                            item.Selected = false;
                            lstVocation2.Items.Add(item);
                            count++;
                            int a = Convert.ToInt32(item.Value);
                            BAL.Vocation.InsertUserVocation(a, Convert.ToInt32(Session["UserID"]));
                            idSpanVocation.Style.Add("display", "none");
                            break;

                        }

                        else
                        {
                            // Response.Write("5 seleted");
                            idSpanVocation.Style.Add("display", "block");
                            idSpanVocation.InnerHtml = "5 Vocations seleted.";
                        }
                    }
                }
            }


        }

        protected void btnRemoveItem_Click(object sender, EventArgs e)
        {
            int count = lstVocation2.Items.Count;
            if (count > 0)
            {
                foreach (ListItem item in lstVocation2.Items)
                {
                    if (item.Selected)
                    {
                        // if (lstVocation2.Items.Contains(item))
                        // {
                        lstVocation2.Items.Remove(item);
                        count--;
                        int a = Convert.ToInt32(item.Value);
                        BAL.Vocation.UpdateUserVocation(Convert.ToInt32(Session["UserID"]), a);
                        idSpanVocation.Style.Add("display", "none");
                        break;
                        // }
                    }
                }
            }


            //for (int i = lstVocation2.Items.Count - 1; i >= 0; i--)
            //{
            //    if (lstVocation2.Items[i].Selected == true)
            //    {
            //        lstVocation1.Items.Add(lstVocation2.Items[i]);
            //        ListItem li = lstVocation2.Items[i];
            //        lstVocation2.Items.Remove(li);
            //    }
            //}
        }

        protected void btnSubmitIndustry_Click(object sender, EventArgs e)
        {
            int industryvalue = Convert.ToInt32(ddlIndustry.SelectedValue);
            if (industryvalue == 0)
            {
                BAL.Vocation.InsertIndustryName(industryvalue, Convert.ToInt32(Session["UserID"]));
                Session["IDIndustry"] = industryvalue;
            }
            else
            {
                BAL.Vocation.UpdateIndustryName(industryvalue, Convert.ToInt32(Session["UserID"]));
                btnAddItem.Visible = true;
            }
        }
    }
}